//************************************************************************
/// <copyrigth>2024-2025 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// Cross-Platform thread pool
///</describe>
/// <date>2024/3/9</date>
///***********************************************************************
#ifndef THREADPOOL_H
#define THREADPOOL_H

#include <functional>
#include <memory>
#include "ModuleLoader/ModuleConfig.h"

namespace SystemTool
{
    enum CLASS_ITEM TDPriority
    {
        TD_LOWEST = 0,
        TD_LOW,
        TD_NORMAL,
        TD_HIGH,
        TD_HIGHEST
    };

    class CLASS_ITEM ThreadPool
    {
    public:
        // Construct the pool
        ThreadPool(int iPoolSize = 16);

        // Destruct the pool
        ~ThreadPool();
    
    private:
        // Copy construct
        ThreadPool(const ThreadPool &other) = delete;

        // Assigment
        ThreadPool &operator=(const ThreadPool &other) = delete;

    public:
        // Start to get a thread to excute task
        void Start(std::function<void(void *)> fFunc, void *pUserData, TDPriority ePreority = TD_NORMAL);

        // Join all
        void JoinAll();

        // Stop all
        void StopAll();

        // Get current pool capacity
        int GetCurCapacity() const;

        // Expand poll
        void ExpandCapacity(int iThreadCnt);

        // Get current available threads
        int GetCurAvailable() const;

    private:
        // Poco thread pool
        void* m_pThreadPool;

        // Pool size
        int m_iPoolSize;
    };
}

#endif // THREADPOOL_H